﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Reflection;


public class Reflect
{
    private NavMeshTest _navMeshTest;

   

    public void Init()
    {
        AssemblyTest();
       
    }

    private void AssemblyTest()
    {
        Assembly assembly = Assembly.LoadFrom(@"F:\Software\Game Software\Unity2018\VSProject\ClassLibrary\ClassLibrary\bin\Debug\netcoreapp3.1\ClassLibrary.dll");
        if(assembly == null)
        {
            Debug.LogError("没有找到dll文件");
        }
        MemberInfo[] infos = assembly.GetTypes();
        for (int i = 0; i < infos.Length; i++)
        {
            Debug.Log(infos[i]);
        }
        Type constsType = assembly.GetType("DllTest");
        MemberInfo[] memberInfos = constsType.GetMembers();
        for (int i = 0; i < memberInfos.Length; i++)
        {
            Debug.Log(memberInfos[i]);
        }
        var constsObj = Activator.CreateInstance(constsType,1,"Alice",100,10,5);
        FieldInfo fieldID = constsType.GetField("ID");
        FieldInfo fieldName = constsType.GetField("Name");
        FieldInfo fieldAttack=constsType.GetField("Attack");
        FieldInfo fieldMaxHp = constsType.GetField("MaxHp");
        FieldInfo fieldDefense = constsType.GetField("Defense");

        MethodInfo methodInfo = constsType.GetMethod("Test");
        methodInfo.Invoke(constsObj,new object[0]);
        MethodInfo methodHit = constsType.GetMethod("HitDamage");
        methodHit.Invoke(constsObj, new object[0]);
   
    }

    private void ActivatorTest()
    {
        Type typeTest = Type.GetType("NavMeshTest");
        var obj = Activator.CreateInstance(typeTest,"小明",1);
        FieldInfo fieldID = typeTest.GetField("ID");
        fieldID.SetValue(obj,99);
        Debug.Log(fieldID.GetValue(obj));
    }

    private void TypeTest()
    {
        _navMeshTest = new NavMeshTest();
        _navMeshTest.ID = 99;

        Type type = Type.GetType("NavMeshTest");
        Type type02 = typeof(NavMeshTest);
        Type type03 = _navMeshTest.GetType();

        Debug.Log(type.Assembly);
        Debug.Log(type02.Assembly);
        Debug.Log(type03.Assembly);

        MemberInfo[] infos = type.GetMembers();
        //for(int i = 0; i < infos.Length; i++)
        //{
        //    Debug.Log(infos[i]);
        //}
        
        ConstructorInfo[] ctors = type.GetConstructors();
        for (int i = 0; i < ctors.Length; i++)
        {
            Debug.Log(ctors[i]);
        }
        //构造函数的调用
        ConstructorInfo ctor = type.GetConstructor(new Type[0]);
        NavMeshTest obj = ctor.Invoke(null) as NavMeshTest;
        Debug.Log(obj.ID);
        ConstructorInfo ctor1 = type.GetConstructor(new Type[] { typeof(int) });
        ctor1.Invoke(new object[] { 2 });
        ConstructorInfo ctor2 = type.GetConstructor(new Type[] { typeof(string), typeof(int) });
        ctor2.Invoke(new object[] { "xiaoming", 3 });

        //变量的调用
        FieldInfo[] fieldinfos = type.GetFields();
        for (int i = 0; i < fieldinfos.Length; i++)
        {
            Debug.Log(fieldinfos[i]);
        }
        FieldInfo fieldID = type.GetField("ID");
        Debug.Log(fieldID.GetValue(_navMeshTest));
        fieldID.SetValue(_navMeshTest, 100);
        Debug.Log(fieldID.GetValue(_navMeshTest));

        //方法的调用
        MethodInfo[] methods = type.GetMethods();
        for (int i = 0; i < methods.Length; i++)
        {
            Debug.Log(methods[i]);
        }

        MethodInfo method = type.GetMethod("Test", new Type[0]);
        method.Invoke(obj, new object[0]);

        method = type.GetMethod("Test", new Type[] { typeof(int), typeof(int) });
        object result = method.Invoke(obj, new object[] { 1, 2 });
        Debug.Log(result);
    }
}
